//
//  DZCommendedViewController.m
//  DaZhePrivateHomeCuisineClient
//
//  Created by zouzhisheng on 14-12-9.
//  Copyright (c) 2014年 zouzhisheng. All rights reserved.
//

#import "DZCommendedViewController.h"
#import "DZSubjectTuijianInfoModel.h"
#import "DZAdviceInfoModel.h"
#import "DZAdviceTableViewCell.h"
#import "DZListTableViewCell.h"
#import "DZArticlesInfoModel.h"
#import "DZWebViewController.h"
@implementation DZCommendedNav
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor clearColor];
    DZCommendedViewController *classVC=[[DZCommendedViewController alloc] init];
    self.viewControllers = @[classVC];
    
}


@end

@interface DZCommendedViewController (){
     NSMutableArray *dataArray;
}

@end

@implementation DZCommendedViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [SharedAppDelegate.home.tabBarController setTabBarHidden:NO];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    dataArray=[NSMutableArray array];
    self.navigationController.navigationBarHidden=YES;
    [self navView];
    [self addTitle:@"大哲推荐"];
    [self loadData];
    // Do any additional setup after loading the view.
}
-(void)loadData{
    NSDictionary *paramDir=[NSDictionary dictionaryWithObjectsAndKeys:@"tuijian",@"act", nil];
    [[BaseService shareNetworkService] requestActivityParameters:paramDir DataSouce:self RequestType:Request_DaZheTuiJianInfo Block:^(__weak id data, NSError *error) {
        if (!error) {
            DZAdviceInfoSubModel *adviceInfoSubModel=SharedAppDelegate.home.adviceInfoModel.adviceInfo[2];
            [dataArray addObject:adviceInfoSubModel];
            DZSubjectTuijianInfoModel *model=data;
            if (model.subjectTuijianInfo.count>0) {
                for (int i=0; i<model.subjectTuijianInfo.count; i++) {
                    [dataArray addObject:model.subjectTuijianInfo[i]];
                }
            }
            [self loadSubView];
        }
    }];

}
-(void)loadSubView
{
    [self.view addSubview:self.listView];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)backAction:(UIButton*)bt
{
    [SharedAppDelegate.home.tabBarController.navigationController popViewControllerAnimated:YES];
}
-(UITableView *)listView
{
    if (!_listView) {
        _listView=[[UITableView alloc] initWithFrame:CGRectMake(0,self.navView.bottom,SCREEN_WIDTH,SYSTEM_VERSION>=7.0?SCREEN_HEIGHT-self.navView.height-49:SCREEN_HEIGHT-self.navView.height-20-49) style:UITableViewStylePlain];
        _listView.delegate=self;
        _listView.dataSource=self;
        _listView.backgroundColor=[UIColor clearColor];
        _listView.separatorStyle=UITableViewCellSeparatorStyleNone;
    }
    return _listView;
}
#pragma mark-UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    id obj=dataArray[indexPath.row];
    if ([obj isKindOfClass:[DZSubjectTuijianInfoSubModel class]]) {
        static NSString *cellIdentifier=@"DZCustomTjTableViewCell";
        DZCustomTjTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (!cell) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"DZCustomTjTableViewCell"  owner:self options:nil] lastObject];
        }
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        [cell loadData:obj];
        return cell;
    }
    if ([obj isKindOfClass:[DZAdviceInfoSubModel class]]) {
        DZAdviceInfoSubModel *model=obj;
        static NSString *cellIdentifier=@"DZAdviceTableViewCell";
        DZAdviceTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (!cell) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"DZAdviceTableViewCell"  owner:self options:nil] lastObject];
        }
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        [cell loadImageUrlStr:model.pic];
        return cell;
    }
    
    return nil;
}

#pragma mark-UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    id obj=dataArray[indexPath.row];
    if ([obj isKindOfClass:[DZSubjectTuijianInfoSubModel class]]) {
        return 90;
    }
    if ([obj isKindOfClass:[DZAdviceInfoSubModel class]]){
//        DZAdviceInfoSubModel *model=obj;
//        CGSize size=[Common downloadImageSizeWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",API_DOMAIN,model.pic]]];
//        float imageH=size.height*SCREEN_WIDTH/size.width;
        return 49;

    }
    return 0;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    id obj=dataArray[indexPath.row];
    if ([obj isKindOfClass:[DZSubjectTuijianInfoSubModel class]]) {
        DZSubjectTuijianInfoSubModel *model=obj;
        DZDinningDetailViewController *dinningDetail=[[DZDinningDetailViewController alloc] init];
        dinningDetail.sid=model.sid;
        [self.navigationController pushViewController:dinningDetail animated:YES];
    }
    if ([obj isKindOfClass:[DZAdviceInfoSubModel class]]) {
        DZAdviceInfoSubModel *model=obj;
        DZWebViewController *webVC=[[DZWebViewController alloc] init];
        webVC.adviceInfoSubModel=model;
        [self.navigationController pushViewController:webVC animated:YES];
    }

    
}
-(void)tableView:(UITableView*)tableView  willDisplayCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexPath
{
    [cell setBackgroundColor:[UIColor clearColor]];
    
}
- (UIView *)viewForLoadAnimationAtService:(RequestType)type{
    return self.view;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
